package com.bzkj.basicinfo.service.basic.impl;

import cn.hutool.Hutool;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import com.bzkj.basicinfo.domain.basic.BasicCustomer;
import com.bzkj.basicinfo.domain.basic.BasicSkuInfo;
import com.bzkj.basicinfo.domain.wms.WmsStoStock;
import com.bzkj.basicinfo.domain.wms.dto.WmsStoStockDto;
import com.bzkj.basicinfo.mapper.BasicSkuInfoMapper;
import com.bzkj.basicinfo.mapper.WmsStoStockMapper;
import com.bzkj.basicinfo.service.basic.BasicSkuInfoService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.model.LoginUser;
import com.bzkj.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author：swj
 * @Desc 商品信息接口
 * @Date：2025/9/28 14:48
 */
@Service
public class BasicSkuInfoServiceImpl implements BasicSkuInfoService {

    @Autowired
    private BasicSkuInfoMapper basicSkuInfoMapper;

    @Autowired
    private WmsStoStockMapper wmsStoStockMapper;

    @Override
    public List<BasicSkuInfo> queryAllList(BasicSkuInfo skuInfo) {
        return basicSkuInfoMapper.selectAllSkuInfo(skuInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(BasicSkuInfo skuInfo) {
        //获取当前登录人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        skuInfo.setCreator(loginUser.getUsername());
        skuInfo.setCreateTime(new Date());
        String uuid = IdUtil.fastSimpleUUID();
        skuInfo.setProductId(uuid);//保证商品id唯一
        skuInfo.setEnableState(0);
        String firstLetter = PinyinUtil.getFirstLetter(skuInfo.getProductName(), "").toUpperCase();
        skuInfo.setMnemonicCode(firstLetter);
        //库存表和商品信息表插入数据
        int i = basicSkuInfoMapper.insertSkuInfo(skuInfo);
        if (i > 0 ) {
            return AjaxResult.success("新增成功");
        }
        return AjaxResult.error("新增失败");
    }

    @Override
    public AjaxResult update(BasicSkuInfo skuInfo) {
        //获取当前登录人
        LoginUser loginUser = SecurityUtils.getLoginUser();
        skuInfo.setModifier(loginUser.getUsername());
        skuInfo.setModifyTime(new Date());
        int i = basicSkuInfoMapper.updateSkuInfo(skuInfo);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    @Override
    public AjaxResult delete(String id) {
        BasicSkuInfo skuInfo = basicSkuInfoMapper.selectById(id);
        if(Objects.nonNull(skuInfo)){
            wmsStoStockMapper.deleteByproductId(skuInfo.getProductId());
        }
        int i = basicSkuInfoMapper.deleteById(id);
        if (i > 0) {
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }

    @Override
    public AjaxResult updateState(String id, String state) {
        int i = basicSkuInfoMapper.updateEnableState(id,state);
        if (i > 0) {
            return AjaxResult.success("修改成功");
        }else{
            return AjaxResult.error("修改失败");
        }
    }
}
